<?php

namespace Roles\Model;

//use Zend\Db\TableGateway\TableGateway;

require_once('nusoap/nusoap.php');
use nusoap_client as SoapClient;

class UsuarioXPerfilTable {

/*   protected $tableGateway;

   public function __construct(TableGateway $tableGateway) {
      $this->tableGateway = $tableGateway;
   }*/

   private $client;

   public function __construct() {
      $this->client = new SoapClient('http://soap.asistencia.uca.edu.py/roles_soap.php');
   }

/*   public function fetchAll() {
      $resultSet = $this->tableGateway->select();
      return $resultSet;
   }*/

   //SOAP Client Version
   public function fetchAll() {

      $result = $this->client->call('usuarioxperfiltable_fetchAll',array());
      //var_dump($result);

      //convertimos la respuesta de array a objetos
      $rowset = json_decode(json_encode($result), FALSE);

      return $rowset;
   }

/*   public function getUsuarioXPerfil($id) {
      $id = (int) $id;
      $rowset = $this->tableGateway->select(array('id' => $id));
      $row = $rowset->current();
      if (!$row) {
         throw new \Exception("Could not find row $id");
      }
      return $row;
   }*/

   //SOAP Client Version
   public function getUsuarioXPerfil($id) {
      $id = (int) $id;

      $result = $this->client->call('getUsuarioXPerfil',array('id' => $id));

      if (empty($result)) {
         throw new \Exception("Could not find row $id");
      }

      $usuarioXperfil = new UsuarioXPerfil();
      $rowset = json_decode(json_encode($result), FALSE);

      $data = array(
         'id'         => $rowset[0]->id,
         'id_usuario' => $rowset[0]->id_usuario,
         'id_perfil'  => $rowset[0]->id_perfil,
      );
      $usuarioXperfil->exchangeArray($data);

      return $usuarioXperfil;
   }

/*   public function saveUsuarioXPerfil(UsuarioXPerfil $usuarioXperfil) {
      $data = array(
         'id_usuario' => $usuarioXperfil->id_usuario,
         'id_perfil'  => $usuarioXperfil->id_perfil,
      );

      $id = (int)$usuarioXperfil->id;
      if ($id == 0) {
         $this->tableGateway->insert($data);
      } else {
         if ($this->getUsuarioXPerfil($id)) {
            $this->tableGateway->update($data, array('id' => $id));
         } else {
            throw new \Exception('id does not exist');
         }
      }
   }*/

   //SOAP Client Version
   public function saveUsuarioXPerfil(UsuarioXPerfil $usuarioXperfil) {
      $data = array(
         'id'         => $usuarioXperfil->id,
         'id_usuario' => $usuarioXperfil->id_usuario,
         'id_perfil'  => $usuarioXperfil->id_perfil,
      );

      $id = (int)$usuarioXperfil->id;
      if ($id == 0) {
         $this->client->call('insertUsuarioXPerfil', $data);
      } else {
         if ($this->getUsuarioXPerfil($id)) {
            $this->client->call('updateUsuarioXPerfil', $data);
         } else {
            throw new \Exception('id does not exist');
         }
      }
   }

/*   public function deleteUsuarioXPerfil($id) {
      $this->tableGateway->delete(array('id' => $id));
   }*/

   //SOAP Client version
   public function deleteUsuarioXPerfil($id) {
      $this->client->call('deleteUsuarioXPerfil', array('id' => $id));
   }

}
